package model.scaling;

import java.util.ArrayList;
import java.util.List;

import model.auxillary.Parameter;
import model.auxillary.ParameterCollectionScaling;
import model.auxillary.ParameterListScaling.ParameterType;

//#Neu
/**
* A {@code Company} is part of the scaling. It allows calculations of 
* relative energy efficiencies of a company.
* @author Sebastian Perlwitz
*
*/
public class Company extends Scaling implements ICompany {
	
	/**
	 * The rank of this {@code Company}. It is used to give parameters a 
	 * unique abbreviation.
	 */
	private int rank;
	
	/**
	 * A List that holds every location that belongs to this company.
	 */
	private ArrayList<ILocation> locations;
	
	/**
	 * To create a new {@code Company}.
	 * @param name the name of the company.
	 * @param id a unique idea that identifies this company.
	 * @param rank the rank of this company.
	 */
	public Company(
			String name,
			String id,
			int rank) {
		super(name, id);
		this.rank = rank;
		locations = new ArrayList<ILocation>();
		addParameters();
	}
	
	@Override
 	public void addLocation(ILocation location) {
 		locations.add(location);
 	}
	
	@Override
	public void removeAllLocationsOfCompany() {
		locations.clear();
	}
	
	@Override
	public ArrayList<ILocation> getLocations() {
		return locations;
	}
 	
 	/**
	 * To add the companies' defining parameters.
     */
	private void addParameters(){
		List<Parameter> newParams = new ArrayList<Parameter>();
		
		newParams.add(new Parameter(
				"Relative Energieeffizienz",
				"REE_Unternehmen," + rank,
				"<html>REE<sub>Unternehmen, " + rank + "</sub></html>",
				"<html>[%]</html>",
				false,
				this));
		
		parameters.addScalingParameters(
				ParameterType.COMPANY_RESULT_PARAMETER, newParams);
	}

	@Override
	public void calculate() {
		
		error = false;
    	aborted = false;
    	
    	double h = 0.0;
    	double E_CUnternehmen = 0.0;
    	
    	for(ILocation location : locations) {
    		double E_CStandort = location.getParameter("E_CStandort",
    				ParameterType.INTERIM_RESULT_PARAMETER).getValue();
    		double REE_Standort = location.getParameter("REE_Standort",
    				ParameterType.LOCATION_RESULT_PARAMETER).getValue();
    		
    		// Change unit of REE_Standort from [%] to [1].
    		REE_Standort /= 100;
    					
    		h += E_CStandort * REE_Standort;
    		E_CUnternehmen += E_CStandort;
    	}
    	
    	double REE_Unternehmen = h / E_CUnternehmen; // [1]
    	REE_Unternehmen *= 100; // [%]
    	
    	getParameter("REE_Unternehmen", 
				ParameterType.COMPANY_RESULT_PARAMETER).
				setValue(REE_Unternehmen);
    }

	@Override
	public boolean aborted() {
	   	return aborted;
    }
	
    @Override
    public boolean getError() {
    	return error;
   	}
    
    @Override
	public Parameter getParameter(String abbreviation, ParameterType type) {

		// An example: To find the parameter 'f_lambda,1' by searching for
		// 'f_lambda'.
		String a = abbreviation + "_" + rank;
		String b = abbreviation + "," + rank;
		Parameter paramA = parameters.getParameter(type, a);
		Parameter paramB = parameters.getParameter(type, b);
		if(paramA == null) {
			return paramB;
		} else {
			return paramA;
		}
	}
	
	@Override
	public List<ParameterCollectionScaling> getResults() {
		List<ParameterCollectionScaling> results =
				new ArrayList<ParameterCollectionScaling>();
		results.add(parameters.getParameterCollection(
				ParameterType.COMPANY_RESULT_PARAMETER));
		return results;
	}
	
}
